// pages/ConsumptionCoupon/index.js
const common = require('../../../public.js')
const utils = require('../../../utils/utils.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isOverShare: true,  //全局分享标识请勿删除
        isTouch: false, //是否可以滚动  true不可
        coupFlag: false,  //控制更多
        show: true,
        managerId: '',
        isLoadingBusy: false,
        cityId: 0,
        regionId: '',
        provinceId: '',
        discountCouponList: [],
        adminDiscountCouponList: [],
        // adminDiscountCouponList:[{"couponNum":1050,"couponName":"枣庄汽车消费券","createTime":1677310939000,"effectiveTime":72,"getNum":1050,"surplusNum":0,"id":924,"managerId":192,"exceedMoney":10000.00,"type":5,"isOffLine":"0","discountsMoney":2000.00},{"couponNum":1300,"couponName":"枣庄汽车消费券","createTime":1677310939000,"effectiveTime":72,"getNum":1300,"surplusNum":0,"id":925,"managerId":192,"exceedMoney":15000.00,"type":5,"isOffLine":"0","discountsMoney":3000.00},{"couponNum":1000,"couponName":"枣庄汽车消费券","createTime":1677310939000,"effectiveTime":72,"getNum":1000,"surplusNum":0,"id":926,"managerId":192,"exceedMoney":20000.00,"type":5,"isOffLine":"0","discountsMoney":4000.00}],
        isCityPage: true,
        from: null,
        imageId: 0,
        active: 0,
        pageNum: 0,
        pageNums: 0,
        desc: "",
        benefitListData: [],
        nomore: true,
        isRealname: 0,
        title: "",
        imgUrl: '',
        flagArr: [],
        activStatus:-1,
        isTouchs:false,
        isGet:false,
    },
    toClose() {
        this.setData({ show: false });
    },
    onClickShow() {
        this.setData({ isTouch: true });
        this.notice()
        this.Gong()
    },
    onClickHide() {
        this.setData({ isTouch: false });
    },
    onClickHides() {
        this.setData({ isTouchs: false });
    },
    noop() { },
    /**
     * 生命周期函数--监听页面加载
     */
    goCoup() {
        wx.navigateTo({
            url: '/pages/cultureCoupons/myCultureCoupons/index',
        })
    },
    goBack() {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
        if (prevPage) {
            wx.navigateBack({
                delta: 1
            })
        } else {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        }
    },
    onChange(event) {
        this.setData({
            active: event.detail.name,
            pageNum: 0
        }, () => {
            if (this.data.active == 0) {  //0是政府券
                console.log("政府券")
                this.getCityCoupons(true)  //政府券
            } else if (this.data.active == 2) {    //2是企业券
                console.log("1211")
                this.getCityList(true)
            }
        })
    },
    // 公告
    notice() {
        var that = this
        common.post('/wlplatfrom/store/xcx/announcement', {}, function (res) {
            if (res.status === 200) {
                //  console.log(res.data,"11111")
                that.setData({
                    title: res.data.data.title,
                    desc: res.data.data.description ? res.data.data.description : '暂无数据'
                })
            } else {
            }
        })
    },

    // 不是枣庄公告
    Gong() {
        var that = this
        common.post('/activExplain/content', {
            activId: that.data.managerId,
            isOnline: that.data.isOffLine,
        }, function (res) {
            if (res.status === 200) {
                console.log(res.data, "11111")
                that.setData({
                    // title:res.data.data.title,
                    descs: res.data ? res.data : '暂无数据'
                })
            } else {
            }
        })
    },
    // 公告
    //   notice_two(){
    //     var that = this
    //     common.post('/wlplatfrom/store/xcx/announcement', {}, function (res) {
    //       if (res.status === 200) {
    //          console.log(res.data,"11111")

    //         } else {

    //     } 
    //   })
    // },
    onLoad: function (options) {
        var that = this
        console.log(options, "kkkkkkhgfd上个页面带来的")
        // 处理实名认证返回没有option的问题
        let capsule = wx.getMenuButtonBoundingClientRect()
        this.setData({
            height: capsule.height,
            top: capsule.top,
            storeId: options.id,
            managerId: options.managerId ? options.managerId : 14,
            isOffLine: options.isOffLine ? options.isOffLine : 0,
            activStatus:options.activStatus ? options.activStatus : -1,
            // imgUrl: options.imgUrl ? options.imgUrl : '',
        }, () => {
            wx.getStorage({
                key: 'user',
                success: function (res) {
                    that.setData({
                        userInfo: res.data.user,
                        token: res.data.token,
                        realname: res.data.user.isRealname == 1
                    }, () => {
                        console.log(that.data.realname)
                    })
                },
                fail: function () {
                    that.setData({
                        userInfo: null
                    })
                }
            })
            if (this.data.active == 2) {
                this.getCityList(true)
                return
            }

            this.getCityCoupons(true)  //政府券
            this.getTopImages()
            // this.moreCoups(true)
        })
    },
    // 去实名
    toRealName() {
        if (this.data.userInfo === null) {
            // 未登录
            common.showLoginPage()
        } else if (this.data.userInfo.isRealname != 1) {
            common.toRealName()
        } else {
            // common.toRealName()
            common.showToast('您已实名')
        }
    },
    onShow: function () {
        const that = this

        // this.notice_two()
    },
    getCouponFinish(e) {
      console.log('00===',e.detail.type);
        if (e.detail.type == 2) {  //资格券
            //惠民券
            this.setData({
                coupFlag: true
            })
            this.setData({ isTouchs: true });
            // this.moreCoups(true)
        }
        // 标记
        this.getCityCoupons()
    },

    loadMore() {
        if (!this.data.active) {  //0是政府券
            console.log("政府券")
            //  this.getCityCoupons(false)  //政府券
        } else {    //2是企业券
            console.log("1211")
            this.getCityList(false)
        }
    },
    // 跳转对应店铺
    toStore(e) {
        common.gotoStore(e.currentTarget.dataset.item)
    },
    /**
     * 获取城市列表
     */
    getCityList(refresh = false) {
        var that = this
        // if(!that.data.nomore)  return  //没有数据禁止请求
        this.setData({
            isLoadingBusy: true
        })
        let params = null
        const pageNum = refresh ? 1 : this.data.pageNum + 1
        params = {
            pageNum,
            pageSize: 10,
            managerId: this.data.managerId
        }
        common.post('/cityDisCoupon/getCityDisCouponFreeStoreListByManagerId', params, function (res) {
            if (res.status === 200) {
                that.data.pageNum = pageNum
                if (!refresh) {
                    if (res.data.length === 0) {
                        that.setData({
                            nomore: false  //无数据不请求
                        })
                    }
                    that.setData({
                        benefitListData: [...that.data.benefitListData, ...res.data]
                    })
                } else {
                    if (res.data.length === 0) {

                    }
                    that.setData({
                        benefitListData: res.data,
                    })
                }

            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                isLoadingBusy: false
            })
        })

    },

    /**
     * 获取优惠券列表
     */
    moreCoups(refresh = false) {
        var that = this
        // if(!that.data.nomore)  return  //没有数据禁止请求
        this.setData({
            isLoadingBusy: true
        })
        const pageNum = refresh ? 1 : this.data.pageNums + 1
        let url = '/cityDisCoupon/getMoreCouponListByManagerId'
        let params = {
            pageNum,
            pageSize: 10,
            managerId: that.data.managerId
        }
        common.post(url, params, function (res) {
            if (res.status === 200) {
                that.data.pageNums = pageNum

                //  if(res.data.discountCouponList.length<10 && res.data.discountCouponList.length<10){
                //   that.setData({
                //     nomore:false  //无数据不请求
                //   })
                //  }
                that.setData({
                    coupList: res.data
                })
            } else {
                // that.setData({
                //   discountCouponList: [],
                //   adminDiscountCouponList: []
                // })
                common.showRequestErrMsg(res)
            }
        },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },
    //  获取顶部广告位图片
    getTopImages() {
        const that = this
        let params = {
            activId: that.data.managerId,
            isOffLine: that.data.isOffLine,
        }
        common.post('/cityDisCoupon/getCouponActivityPicByActivId', params, function (res) {
            if (res.status === 200) {
                console.log(res.data)
                that.setData({
                    imgUrl:res.data
                })
            }
        })
    },
    getCityCoupons(refresh = false) {
        var that = this
        // if(!that.data.nomore)  return  //没有数据禁止请求
        this.setData({
            isLoadingBusy: true
        })

        const pageNum = refresh ? 1 : this.data.pageNum + 1
        let url = '/cityDisCoupon/getMoreCouponListByActivId'
        let params = {
            activId: that.data.managerId,
            isOffLine: that.data.isOffLine,
        }
        common.post(url, params, function (res) {
            if (res.status === 200) {
                that.data.pageNum = pageNum
                if (res.data.length < 10) {
                    that.setData({
                        nomore: false  //无数据不请求
                    })
                }
                that.setData({
                    flagArr: res.data ? res.data : []
                })
                var qualificationsArr = res.data.filter((x) => x.type == 4)   //资格券 
                var admiDiscoun = res.data.filter((x) => x.type !== 4)   //资格券 
                // 0 线下 1线上
                if (that.data.isOffLine == 0) {
                    that.setData({
                        // discountCouponList: res.data.discountCouponList,
                        adminDiscountCouponList: admiDiscoun,
                        qualificationsArr
                    })
                } else {
                    that.setData({
                        discountCouponList: admiDiscoun,
                        qualificationsArr
                        // adminDiscountCouponList: res.data.adminDiscountCouponList
                    })
                }

            } else {
                that.setData({
                    discountCouponList: [],
                    adminDiscountCouponList: []
                })
                common.showRequestErrMsg(res)
            }
        },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
            })
    },
    toDetail(e) { // 跳转详情页
        let dcId = e.currentTarget.dataset.id
     
        wx.navigateTo({
            url: '/packageB/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + dcId
        })
    },
    toPlatformCoupons(e) {
        let { flags, id } = e.currentTarget.dataset
        wx.navigateTo({
            url: `/packageB/pages/cultureCoupons/platformCoupons/index?dcId=${id}&flag=${flags}&isOffLine=${this.data.isOffLine}`
        })
    },
    getTopicTitle() {
        return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
    },
    getJumpParma() {
        let tmpList = []
        !common.isEmptyString(this.data.provinceId) && tmpList.push('provinceId=' + this.data.provinceId)
        !common.isEmptyString(this.data.cityId) && tmpList.push('cityId=' + this.data.cityId)
        !common.isEmptyString(this.data.regionId) && tmpList.push('regionId=' + this.data.regionId)
        return tmpList.join('&')
    },

    /**
    * 用户点击右上角分享
    */
    // onShareAppMessage: function () {
    //     return {
    //         title: '补贴来了，海量消费券，等你来抢！',
    //         imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/share.jpg',
    //         success: (res) => { },
    //         fail: (res) => {
    //             // 分享失败
    //         }
    //     }
    // },
    // onShareTimeline() {
    //     const query = 'from=timeline&' + this.getJumpParma()
    //     return {
    //         title: '补贴来了，海量消费券，等你来抢！',
    //         query
    //     }
    // },



    onShareAppMessage: function (res) { // 分享转发
        console.log('点击了---isOffLine--',this.data.isOffLine,'====managerId==',this.data.activId,'----activStatus---',this.data.activStatus);

        // let awardNums = this.data.drawInfo.awardNums
        // let awardName = this.data.drawInfo.awardName
        return {
            title: '补贴来了，海量消费券，等你来抢！',
            path: 'packageC/pages/coupCenterGet/coupCenterGet?isOffLine=' + this.data.isOffLine +
            '&managerId=' + this.data.managerId + '&activStatus=' + this.data.activStatus,
            imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/share.jpg',


        }

    },

    // 朋友圈
    onShareTimeline() {

        // let id = this.data.drawInfo.id
        // let inviteUserId = this.data.userInfo.id
        // let awardNums = this.data.drawInfo.awardNums
        // let awardName = this.data.drawInfo.awardName
        let data = {
            // query: 'drawId=' + id
            //     + '&inviteUserId=' + inviteUserId,
            path: 'packageC/pages/coupCenterGet/coupCenterGet?isOffLine=' + this.data.isOffLine +
            '&managerId=' + this.data.managerId + '&activStatus=' + this.data.activStatus,
            imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/share.jpg',
            title: '补贴来了，海量消费券，等你来抢！',
            success: function (res) {
                console.log("jdsjsdsdj", res)
            }
        }
        return data

    }




    

})